package com.eastfair.transport.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.annotation.annotation.log.SysLog;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.echo.core.EchoService;
import com.eastfair.transport.dto.CustomsClearanceDTO;
import com.eastfair.transport.dto.CustomsClearancePageQuery;
import com.eastfair.transport.dto.CustomsClearanceSaveDTO;
import com.eastfair.transport.dto.CustomsClearanceUpdateDTO;
import com.eastfair.transport.entity.CustomsClearance;
import com.eastfair.transport.service.CustomsClearanceService;
import com.eastfair.transport.vo.CustomsClearanceVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 清关商品
 * </p>
 *
 * @author linan
 * @date 2022-07-21
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/customsClearance")
@Api(value = "CustomsClearance", tags = "清关商品")
@PreAuth(replace = "transport:customsClearance:")
public class CustomsClearanceController extends SuperController<CustomsClearanceService, Long, CustomsClearance, CustomsClearancePageQuery, CustomsClearanceSaveDTO, CustomsClearanceUpdateDTO> {

    @Autowired
    private EchoService echoService;

    @Override
    public void handlerResult(IPage<CustomsClearance> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<CustomsClearance> customsClearanceList = list.stream().map((map) -> {
            CustomsClearance customsClearance = CustomsClearance.builder().build();
            //TODO 请在这里完成转换
            return customsClearance;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(customsClearanceList));
    }



    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<CustomsClearancePageQuery> params) {
        CustomsClearancePageQuery pageQuery = params.getModel();
        //分页处理
        if(ContextUtil.getProjectId()!=null){
            pageQuery.setProjectId(ContextUtil.getProjectId());
        }
    }

    /**
     * 添加清关商品
     *
     * @param customsClearanceDTO 添加清关商品
     * @return R
     */
    @ApiOperation(value = "添加清关商品", notes = "添加清关商品")
    @SysLog("添加清关商品")
    @PostMapping("/addCustomsClearance")
    public R<Boolean> addCustomsClearance(@RequestBody CustomsClearanceDTO customsClearanceDTO) {
        log.info("customsClearanceDTO - 添加清关商品 customsClearanceDTO={}", customsClearanceDTO);
        try {
            return baseService.addCustomsClearance(customsClearanceDTO);
        } catch (Exception e) {
            return R.fail(e);
        }
    }

    /**
     * 编辑清关商品
     *
     * @param customsClearanceDTO 编辑清关商品
     * @return R
     */
    @ApiOperation(value = "编辑清关商品", notes = "编辑清关商品")
    @SysLog("修改清关商品")
    @PostMapping("/updateCustomsClearance")
    public R<Boolean> updateCustomsClearance(@RequestBody CustomsClearanceDTO customsClearanceDTO) {
        log.info("updateCustomsClearance - 编辑清关商品 customsClearanceDTO={}", customsClearanceDTO);
        try {
            return baseService.updateCustomsClearance(customsClearanceDTO);
        } catch (Exception e) {
            return R.fail(e);
        }
    }

    /**
     * 清关商品列表
     *
     * @param customsClearancePageQuery 清关商品列表
     * @return R
     */
    @ApiOperation(value = "清关商品列表", notes = "清关商品列表")
    @PostMapping("/queryCustomsClearance")
    public R<IPage<CustomsClearanceVO>> queryCustomsClearance(@RequestBody PageParams<CustomsClearancePageQuery> customsClearancePageQuery) {
        log.info("queryCustomsClearance - 清关商品列表 customsClearancePageQuery={}", customsClearancePageQuery);
        try {
            return baseService.queryCustomsClearance(customsClearancePageQuery);
        } catch (Exception e) {
            return R.fail(e);
        }
    }

    /**
     * 删除清关商品
     *
     * @param customsClearanceDTO 删除清关商品
     * @return R
     */
    @ApiOperation(value = "删除清关商品", notes = "删除清关商品")
    @SysLog("删除清关商品")
    @PostMapping("/delCustomsClearance")
    public R<Boolean> delCustomsClearance(@RequestBody List<CustomsClearanceDTO> customsClearanceDTO) {
        log.info("queryCustomsClearance - 删除清关商品 customsClearanceDTO={}", customsClearanceDTO);
        try {
            return R.success(baseService.delCustomsClearance(customsClearanceDTO));
        } catch (Exception e) {
            return R.fail(e);
        }
    }

    /**
     * 修改清关商品状态
     *
     * @param customsClearanceDTO 修改清关商品状态
     * @return R
     */
    @ApiOperation(value = "修改清关商品状态", notes = "修改清关商品状态")
    @SysLog("修改清关商品状态")
    @PostMapping("/updateAuditState")
    public R<Boolean> updateAuditState(@RequestBody CustomsClearanceDTO customsClearanceDTO) {
        log.info("updateAuditState - 修改清关商品状态 customsClearanceDTO={}", customsClearanceDTO);
        try {
            CustomsClearance customsClearance = new CustomsClearance();
            customsClearance.setId(customsClearanceDTO.getId());
            customsClearance.setAuditState(customsClearanceDTO.getAuditState());
            return R.success(baseService.updateById(customsClearance));
        } catch (Exception e) {
            return R.fail(e);
        }
    }

}
